package com.ay272.sshupdate.web;

import com.ay272.sshupdate.biz.QianDaoBiz;
import com.ay272.sshupdate.entity.QianDao;

import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by java on 2016/5/27.
 */
@Controller
@RequestMapping("/qian")
public class QianDaoController {
    @Autowired
    private QianDaoBiz biz;
    @RequestMapping("/save")
    @ResponseBody
    public QianDao save(HttpServletRequest request){
        QianDao qian=new QianDao();
        String addr=request.getRemoteAddr();
        qian.setIp(addr);
        SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        qian.setTime(f.format(new Date()));
        biz.save(qian);
        return qian;
    }
    @RequestMapping("/daochu")
    public void daochu(HttpServletResponse response){
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition","attachment;filename=qianDao.xls");
        List<QianDao>list=biz.list();
        try(InputStream is=QianDaoController.class.getClassLoader().getResourceAsStream("xls/kaoqing2.xls")) {
          try(OutputStream out=response.getOutputStream()) {
              Context context = new Context();
              context.putVar("qianDaos",list);
              JxlsHelper.getInstance().processTemplate(is,out,context);
          }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
